import { hasToken } from '@/utils/storage';
import { Route, Redirect, RouteProps } from 'react-router-dom';

export type LocationStateType = {
  fromPath: string;
};

export default function AuthRoute({
  path,
  component: Component,
  ...restProps
}: IAuthRouteProps) {
  if (!Component) {
    console.log('component必须要有');
    return null;
  }

  return (
    <Route
      {...restProps}
      path={path}
      render={(_props: any) => {
        if (hasToken()) {
          return <Component {..._props} />;
        }
        return <Redirect to={{ pathname: '/login', state: { fromPath: path } }} />;
      }}
    />
  );
}

interface IAuthRouteProps extends RouteProps {}
